package cn.raven.basic.controller;

import cn.raven.basic.dto.VerifyRegisterDto;
import cn.raven.basic.exception.BusinessException;
import cn.raven.basic.service.IVerifyCodeService;
import cn.raven.basic.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**

* @Description: 验证
* @Author: king
* @Version: 1.0
* @Date:  2022/7/4 16:01  
  */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;
    
    /** 
    * @Title: captcha
    * @Description: 图形验证码
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/4 16:02  
    * @Parameters: [imgCodeKey]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult captcha(@PathVariable("imageCodeKey") String imageCodeKey){
        try {
            String imgBase64 = verifyCodeService.captcha(imageCodeKey);
            return AjaxResult.me().setResultObj(imgBase64);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,骚后重试");
        }
    }

    /**
    * @Title: smsCode
    * @Description: 发送短信验证码
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/6 11:38
    * @Parameters: [verifyRegisterDto]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody VerifyRegisterDto verifyRegisterDto){
        try {
            verifyCodeService.smsCode(verifyRegisterDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,骚后重试");
        }
    }

}
